﻿function alertWin(title,  w, h){ 
	var titleheight = "23px"; // 窗口标题高度 
	var bordercolor = "#336699"; // 提示窗口的边框颜色 
	var titlecolor = "#FFFFFF"; // 窗口标题颜色 
	var titlebgcolor = "#336699"; // 窗口标题背景色
	var bgcolor = "#FFFFFF"; // 提示内容的背景色
	
	var iWidth = document.documentElement.clientWidth; 
	var iHeight = document.documentElement.clientHeight; 
	
	var contentDiv = document.getElementById("quickbuy");
	
	var bgObj = document.createElement("div"); 
	bgObj.id = "bgObjId";
	bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+iWidth+"px;height:"+Math.max(document.body.clientHeight, iHeight)+"px;filter:Alpha(Opacity=30);opacity:0.3;background-color:#000000;z-index:100000;";
	document.body.appendChild(bgObj); 
	
	//var msgObj=document.createElement("div");
	
	contentDiv.style.cssText = "position:absolute;font:11px '宋体';top:"+(iHeight-h)/2+"px;left:"+(iWidth-w)/2+"px;width:"+w+"px;height:"+h+"px;text-align:center;border:1px solid "+bordercolor+";background-color:"+bgcolor+";padding:1px;line-height:22px;z-index:100001;";
	document.body.appendChild(contentDiv);
	
	
	var table = document.createElement("table");
	table.id = "tableId";
	$('quickbuytop').parentNode.insertBefore(table,$('quickbuytop'));
	//contentDiv.appendChild(table);
	table.style.cssText = "margin:0px;border:0px;padding:0px;";
	table.cellSpacing = 0;
	var tr = table.insertRow(-1);
	var titleBar = tr.insertCell(-1);
	titleBar.style.cssText = "width:100%;height:"+titleheight+"px;text-align:left;padding:3px;margin:0px;font:bold 13px '宋体';color:"+titlecolor+";border:1px solid " + bordercolor + ";cursor:move;background-color:" + titlebgcolor;
	titleBar.style.paddingLeft = "10px";
	titleBar.innerHTML = title;
	var moveX = 0;
	var moveY = 0;
	var moveTop = 0;
	var moveLeft = 0;
	var moveable = false;
	var docMouseMoveEvent = document.onmousemove;
	var docMouseUpEvent = document.onmouseup;
	titleBar.onmousedown = function() {
		var evt = getEvent();
		moveable = true; 
		moveX = evt.clientX;
		moveY = evt.clientY;
		moveTop = parseInt(contentDiv.style.top);
		moveLeft = parseInt(contentDiv.style.left);
		
		document.onmousemove = function() {
			if (moveable) {
				var evt = getEvent();
				var x = moveLeft + evt.clientX - moveX;
				var y = moveTop + evt.clientY - moveY;
				if ( x > 0 &&( x + w < iWidth) && y > 0 && (y + h < iHeight) ) {
					contentDiv.style.left = x + "px";
					contentDiv.style.top = y + "px";
				}
			}	
		};
		document.onmouseup = function () { 
			if (moveable) { 
				document.onmousemove = docMouseMoveEvent;
				document.onmouseup = docMouseUpEvent;
				moveable = false; 
				moveX = 0;
				moveY = 0;
				moveTop = 0;
				moveLeft = 0;
			} 
		};
	}
	
	var closeBtn = tr.insertCell(-1);
	closeBtn.style.cssText = "cursor:pointer; padding:2px;background-color:" + titlebgcolor;
	closeBtn.innerHTML = "<span style='font-size:15pt; color:"+titlecolor+";'>×</span>";
	closeBtn.onclick = function(){ 
		document.body.removeChild(bgObj); 
		contentDiv.removeChild(table);
		contentDiv.style.display = 'none';
		//document.body.removeChild(contentDiv); 
	} 
	//var msgBox = table.insertRow(-1).insertCell(-1);
	//msgBox.style.cssText = "font:10pt '宋体';";
	//msgBox.colSpan  = 2;
	//msgBox.innerHTML = contentDiv.innerHTML;
	
    // 获得Event对象，用于兼容IE和FireFox
    function getEvent() {
	    return window.event || arguments.callee.caller.arguments[0];
    }

}

var sUrl = "http://www.applelew.cn/BARIOR/getSelectGood.do";
//var sUrl = "http://localhost:8080/vancl/getSelectGood.do";
function makeRequest(gid,cid,size,num){
	var postData = "goodId="+gid+"&colorId="+cid+"&size=-1&num=-1";
	//alert(postData);
	var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}
var result = null;
var handleSuccess = function(o){

	if(o.responseText !== undefined){
		//var str = {"two":"2","one":"1"};
		
		//alert(o.responseText);
		result = YAHOO.lang.JSON.parse(o.responseText);
		
		
		
		
		/*
		var cProductNum = YAHOO.util.Cookie.get("cProductNum");
		var cProductPrice = YAHOO.util.Cookie.get("cProductPrice");
		if(cProductNum == null){
			YAHOO.util.Cookie.set("cProductNum", result.num);
			YAHOO.util.Cookie.set("cProductPrice", result.allPrice);
			
		}else{
			cProductNum = parseFloat(cProductNum) + parseFloat(result.num);
			cProductPrice = parseFloat(cProductPrice) + parseFloat(result.allPrice);
			
		YAHOO.util.Cookie.set("cProductNum", cProductNum);
		YAHOO.util.Cookie.set("cProductPrice", cProductPrice);
		
		*/
		//alert(result);
		$('gQuickName').innerHTML = result.goodName;
		$('quickColor').innerHTML = result.colorName;
		$('quickOPrice').innerHTML = result.oriPrice;
		$('quickCPrice').innerHTML = result.currentPrice;
		$('qChooseColor').innerHTML = result.colorName;
		$('quickSell').innerHTML = result.goodSell;
		var qLiStr = "";
		
		var picList = result.ps;
		var firstPic = "resource/products/" + result.quickPic;
		//alert(firstPic);
		for(var i=0;i<picList.length;i++){
			var picObj = picList[i];
			//if(i == 0)
			//	firstPic = "resource/products/" + picObj.prd_normal_pic;
			//alert(picObj);
			var picSrc = "resource/products/" + picObj.prd_simple_pic;
			var liStr = "<li><img class=\"indexImgout\" src=\""+picSrc+"\"  border=0 onmouseover=\"Javascript:this.className='indexImgCur';setPic('"+picObj.prd_normal_pic+"');\" onmouseout=\"Javascript:this.className='indexImgout';\"></li>";	
			qLiStr = qLiStr + liStr;
		}
		//alert(qLiStr);
		$('quickPicList').innerHTML = qLiStr;
		$('gQuickPic').src = firstPic;
		
		//设置尺寸
		var sizeList = result.goodSize;
		var sizeLiStr = "";
		for(var i=0;i<sizeList.length;i++){
			var sizeObj = sizeList[i];
			var sizeStr = "<li><div class=\"sizecode\" onmouseover=\"this.className='sizecodeCur';\" onmouseout=\"this.className='sizecode';\" onclick=\"setQSize('"+sizeObj.content+"')\" >"+sizeObj.content+"</div></li>";
			sizeLiStr = sizeLiStr + sizeStr;
		}
		$('quickSize').innerHTML = sizeLiStr;
		alertWin('快速查看',400,500);
		//alert(result.colorId);
	}
}


function rollUp(){
	
	Effect.BlindUp('shoppingbag',{ duration: 1.0 });
	//window.setTimeout('eleBlock()',1000*1);
}

function eleBlock(){
		$('headerContentBottom').style.display = 'block';
}

/**
添加到购物车
*/
function addToBag(){
		
		var limit = 4;
		
		var goodStr = "";
		for(var i=0;i<limit;i++){
			var tKey = "cProduct_"+i;
			var tGStr = Store.get(tKey);
			if(tGStr != null && tGStr != ""){
			//	alert(i);
				goodStr = goodStr + tGStr;
			}
		}
		
		//alert(goodStr.length);
		$('gEleName').innerHTML =	result.goodName;	//商品名称
		$('gEleId').innerHTML = result.colorId;	//版色编号
		$('gEleColor').innerHTML = result.colorName;		//版色
		$('gEleSize').innerHTML = $('qSizeONe').innerHTML;		//尺码
		//alert($('elePrice').value);
		var elePrice = result.currentPrice;
		$('gElePrice').innerHTML = elePrice;	//价格
		//alert($('buynum').value);
		var buynum = $('buynum').value;
		$('gEleQty').innerHTML = buynum;	//数量
		var allPrice = elePrice * buynum;
		$('gEleAllPrice').innerHTML = allPrice;	//总价
		$('gElePic').src = "resource/products/" + result.checkPic;	//结算图片
		
		
		//设置result 的数量 尺寸 
		result.num = buynum;
		result.size = $('qSizeONe').innerHTML;
		var tGoodStr = YAHOO.lang.JSON.stringify(result);
		//alert(tGoodStr);
		if(goodStr == null || goodStr == ""){
			goodStr = tGoodStr;
			//alert("aa" + goodStr.length);
		}else{
			//alert(o.responseText);
			goodStr = goodStr + "," + tGoodStr;
			//alert(goodStr.length);
		}
		
		
		//update by applelew for cookie limit 4096byte
		var cUse = Math.ceil(goodStr.length / 60000);
		for(var i=0;i<cUse;i++){
			var tmpGoodStr = goodStr.substr(i*60000,60000);
			//alert("i: " + i + "--->" + tmpGoodStr.length);
			var tKey = "cProduct_"+i;
			Store.set(tKey, tmpGoodStr);
			
			//var mmm = YAHOO.util.Cookie.get(tKey);
			//alert(tKey + "---> " +mmm.length);
		}
		//YAHOO.util.Cookie.set("cProduct", goodStr);
		
		
		
		var cProductNum = Store.get("cProductNum");
		var cProductPrice = Store.get("cProductPrice");
		var ctProductNum = parseFloat(cProductNum) + parseFloat(buynum);
		var ctProductPrice = parseFloat(cProductPrice) + parseFloat(allPrice);
		
		if(cProductNum == null || cProductNum == ""){
			//alert(buynum);
			//alert(ctProductNum);
			$('cartNum').innerHTML = buynum;
			$('cartPrice').innerHTML = allPrice;
			
			Store.set("cProductNum", buynum);
			Store.set("cProductPrice", allPrice);
			
		}else{
			$('cartNum').innerHTML = ctProductNum;
			$('cartPrice').innerHTML = ctProductPrice;
			
			Store.set("cProductNum", ctProductNum);
			Store.set("cProductPrice", ctProductPrice);
		
		}	
		
		var contentDiv = $('quickbuy');
		var table = $('tableId');
		var bgObj = $('bgObjId');
		
		document.body.removeChild(bgObj); 
		contentDiv.removeChild(table);
		contentDiv.style.display = 'none';
		
		//$('headerContentBottom').style.display = 'none';
		Effect.BlindDown('shoppingbag',{ duration: 1.0 });
		getGoodNum();
		window.setTimeout('rollUp()',1000*5);
}

var handleFailure = function(o){

	if(o.responseText !== undefined){
		//TODO add error process
	}
}

function setQSize(qsize){
	$('qChooseSize').innerHTML = qsize;
	$('qSizeONe').innerHTML = qsize;
}

function setQChooseNum(choose){
	$('qChooseNum').innerHTML = choose;
}

function setPic(picSrc){
	
	$('gQuickPic').src = "resource/products/" + picSrc;
	//alert($('gQuickPic').src);
}

var goodNum = 0;
function getGoodNum(){
	
	
	var cNum = Store.get("cProductNum");
	if(cNum != null && cNum != ""){
		//alert(cNum);
		goodNum = cNum;
	}
	$('goodNum').innerHTML = goodNum;
	
}



var callback =
{
  success:handleSuccess,
  failure: handleFailure,
  argument: ['foo','bar']
};

//makeRequest();

